package scu.debate.service.contest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scu.debate.dao.DAO.ContestDao;
import scu.debate.dao.entity.Arguement;
import scu.debate.dao.entity.Contest;
import scu.debate.service.argument.ArgumentService;

import java.util.List;

@Service
public class ContestService {
    @Autowired
    private ContestDao contestDao;
    @Autowired
    private ArgumentService argumentService;

    public List<Contest> getAllContest() {
        return contestDao.queryAll();
    }

    public List<Contest> getContestByArgumentId(Integer argumentId) {
        return contestDao.selectByArgumentId(argumentId);
    }

    public boolean checkRepeatContest(Contest contest){
        return null != contestDao.queryContestLimit(contest);
    }

    public Contest getContestById(Integer contestid){
        Contest contest = new Contest();
        contest.setContestid(contestid);
        return contestDao.selectByPrimaryKey(contestid);
    }

    public Contest addContest(Contest contest){
        boolean check = checkRepeatContest(contest);
        if(check){
            return null;
        }
        if(contestDao.insert(contest) != 1) {
            return null;
        }
        return contest;
    }

    public Contest deleteContestById(Integer contestid){
        Contest query = getContestById(contestid);
        if(query == null){
            return null;
        }
        contestDao.deleteByPrimaryKey(contestid);
        return query;
    }

    public Contest updateContest(Contest contest){
        Contest query = getContestById(contest.getContestid());
        if( null == query){
            return null;
        }
        contestDao.updateByPrimaryKey(contest);
        return contest;
    }

    public List<Arguement> getArgumentByCname(String contestname){
        List<Integer> argumentIdList = contestDao.getArgumentId(contestname);
        return argumentService.getByContestName(argumentIdList);
    }


    public List<String> getAllContestname(){
        return contestDao.getAllContestname();
    }
}
